package com.luoran.bartalks.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 时间操作工具类
 * 
 * @author M1nG
 * @date 2018年12月27日
 */
public class DateFormatUtils {

	private static final Logger log = LoggerFactory.getLogger(DateFormatUtils.class);

	public static Date Str2Date(String dateStr) {
		Date date = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			date = sdf.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("字符串转日期失败！");
		}
		return date;
	}

	public static Date Str2DateTime(String dateStr) {
		Date date = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			date = sdf.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("字符串转时间失败！");
		}
		return date;
	}

	public static Date Str2DateTimefinalNow(String dateStr) {
		Date date = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			date = sdf.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
			date = new Date();
			log.info("字符串转时间失败！返回当前时间。");
		}
		return date;
	}

	public static String Date2Str(Date date) {
		String dateStr = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			dateStr = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("时间转日期字符串失败！");
		}
		return dateStr;
	}

	public static String DateTime2Str(Date date) {
		String dateStr = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dateStr = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("时间转时间字符串失败！");
		}
		return dateStr;
	}

	public static String DateTime2StrFinalNow(Date date) {
		String dateStr = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dateStr = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			dateStr = DateTime2Str(new Date());
			log.info("时间转时间字符串失败！返回当前时间字符串。");
		}
		return dateStr;
	}

	public static String Date2StrFinalNow(Date date) {
		String dateStr = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			dateStr = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			dateStr = Date2Str(new Date());
			log.info("日期转日期字符串失败！返回当前日期字符串。");
		}
		return dateStr;
	}

	/**
	 * 时间变更计算,依照参数标示进行对应的计算
	 *
	 * @param time{day/天，month/月，year/年}
	 * @param num{-负数为减代表过去时间，+正数为加法代表未来的时间}
	 * @return dataFmtStr{yyyy-MM-dd HH:mm:ss}
	 */
	public static String dateMath(String time, int num) {
		String timeMath = null;
		// 时间操作
		Calendar calendar = Calendar.getInstance();
		Date dateNow = new Date();
		if (time.equals("day")) {
			// 过去七天
			calendar.setTime(dateNow);
			calendar.add(Calendar.DATE, num);
			Date dateReckon = calendar.getTime();
			timeMath = DateTime2Str(dateReckon);
		} else if (time.equals("month")) {
			// 过去一月
			calendar.setTime(new Date());
			calendar.add(Calendar.MONTH, num);
			Date dateReckon = calendar.getTime();
			timeMath = DateTime2Str(dateReckon);
		} else if (time.equals("year")) {
			// 过去一年
			calendar.setTime(new Date());
			calendar.add(Calendar.YEAR, num);
			Date dateReckon = calendar.getTime();
			timeMath = DateTime2Str(dateReckon);
		} else {
			timeMath = null;
		}
		return timeMath;
	}

	/**
	 * 时间计算月的数量和周的数量
	 * 
	 * @param data1 开始时间
	 * @param data2 结束时间
	 * @param type  {day/天，month/月，year/年，week/周}
	 * @return 如果type无匹配或者结束时间比开始时间还早返回0
	 */
	public static int dateMathNumber(Date data1, Date data2, String type) {
		Calendar from = Calendar.getInstance();
		from.setTime(data1);
		Calendar to = Calendar.getInstance();
		to.setTime(data2);
		
		int fromYear = from.get(Calendar.YEAR);
		int fromMonth = from.get(Calendar.MONTH);

		int toYear = to.get(Calendar.YEAR);
		int toMonth = to.get(Calendar.MONTH);

		int year = toYear - fromYear;
		int month = toYear * 12 + toMonth - (fromYear * 12 + fromMonth);
		int day = (int) ((to.getTimeInMillis() - from.getTimeInMillis()) / (24 * 3600 * 1000));
		int week = day % 7 == 0 ? day / 7 : day / 7 + 1;
		int num;
		switch (type) {
		case "year":
			num = year;
			break;
		case "month":
			num = month;
			break;
		case "week":
			num = week;
			break;
		case "day":
			num = day;
			break;
		default:
			num = 0;
			break;
		}
		return num;

	}

	/**
	 * 2个时间比较最大的
	 * 
	 * @param data1
	 * @param data2
	 * @return
	 */
	public static Date dateCompare(Date data1, Date data2) {
		Date date = new Date();
		if (data1 != null && data2 != null) {
			int cmp = 0;
			try {
				cmp = ((int) (data1.getTime() / 1000)) - ((int) (data2.getTime() / 1000));
			} catch (Exception e) {
				e.printStackTrace();
				log.error("时间比较异常");
			}
			if (cmp < 0) {
				date = data2;
			} else if (cmp > 0) {
				date = data1;
			} else {
				return data1;
			}
		} else {
			if (data1 == null && data2 == null) {
				try {
					throw new NullPointerException("all param is null");
				} catch (Exception e) {
					e.printStackTrace();
					log.error("传入时间参数均为null");
				}
			} else {
				if (data1 == null) {
					date = data2;
				}
				if (data2 == null) {
					date = data1;
				}
			}
		}
		return date;
	}

}
